package service;

import dao.FileDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/delete")
public class DeleteFile extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取要删除的文件ID
        String fileId = req.getParameter("fileId");
        int i ;
        // 根据文件ID从数据库中删除对应的文件记录
        try {
            i = FileDao.delete(fileId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // 判断是否删除成功
        if (i >= 1) {
            // 删除成功后，重定向到文件列表页面
            resp.sendRedirect("list.jsp");
        } else {
            // 删除失败，返回错误信息
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "删除文件失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}
